package com.awan.utils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;

import com.beem.project.beem.BeemApplication;
import com.beem.project.beem.BeemService;
import com.beem.project.beem.R;

public class Constanta {
	
	/*
	 * SQLite
	 * */
	public static final String DATABASE_NAME = "SMILES.db";
	public static final int DATABASE_VERSION = 2;
	
	public static final String chatLogTable = "chat";
	public static final String chatRecentTable = "recent_chat";
	
	/*
	 * main domain
	 * development
	 * */
	/*public static String chatDomain = "dev.digitalbuana.com";
	public static String chatSecretKey="0aL3xvY0";*/
	
	/*
	 * main domain
	 * live
	 * */
	public static String chatDomain = "202.78.199.248";
	public static String chatSecretKey="Xlnive2k";//"DPvU4U0V";
	
	public static String apiDomain = "202.78.199.249";
	
	/*
	 * API parent URL
	 * */
	public static String apiUrl = "http://"+apiDomain;
	public static String apiPath = apiUrl+"/";
	public static String apiUserPath = apiPath+"/user";
	public static String apiFriendPath = apiPath+"/friend";
	public static String apiTransactionPath = apiPath+"/transaction";
	public static String apiFilePath = apiPath+"/file";
	public static String apiUserDirPath = apiPath+"/user_dir";
	public static String apiStickerPath = apiPath+"/sticker";
		
	/*
	 * API child URL
	 * */
	public static String apiRegister = apiUserPath+"/register";
	public static String apiUploadAvatar = apiUserPath+"/upload_avatar";
	public static String apiUserProfile = apiUserPath+"/get_profile";
	public static String apiUserList = apiFriendPath+"/public";//"/public";
	public static String apiGetTicketProperties = apiTransactionPath+"/generate_ticket";
	public static String apiGetTicketStatus = apiTransactionPath+"/check_ticket";
	public static String apiSendFile = apiFilePath+"/send";
	public static String apiGetStickerCategory = apiStickerPath+"/packages";
	public static String apiGetStickerDetail = apiStickerPath+"/package_detail";
	public static String apiGetUserSearch = apiUserPath+"/search";
	public static String apiSetUserFriend = apiFriendPath+"/add";
	public static String apiFriendList = apiFriendPath+"/list/active";
	public static String apiFriendRequest = apiFriendPath+"/list/request";
	public static String apiFriendApprove = apiFriendPath+"/approve";
	public static String apiSetUserStatus = apiUserPath+"/update_status";
	
	/*
	 * API radio list tmp?
	 * */
	public static String radioUrlList = "http://202.78.199.248/smiles_radio_list.php?passwd=P@ssw0rd";
	
	/*
	 * API tv list tmp?
	 * */
	public static String tvUrlList = "http://202.78.199.248/smiles_tv_list.php?passwd=P@ssw0rd";
	
	/*
	 * xmpp server
	 * http://[domain]:9090/plugins/userService/userservice?type=add&secret=[privatekey]&username=[username]&password=[password]&name=[name]&email=[mail]
	 * */
	public static String ofAddUser = apiUrl+":"+BeemService.DEFAULT_XMPP_PORT+
			"/plugins/userService/userservice?type=add&secret=[privatekey]&username=[username]&password=[password]&name=[name]&email=[mail]";
	
	public static int windowHeight;
	public static int windowsWidth;
	public static int density;
	
	private static Map<String, String> httpHeader = null;
	public static Map<String, String> requestHeaders(){		
		if(httpHeader != null)
			return httpHeader;		
		Map<String, String> requestHeaders = new HashMap<String, String>();
		requestHeaders.put("User-Agent", "Android-");
		requestHeaders.put("Content-Type", "application/x-www-form-urlencoded");
		requestHeaders.put("Accept_Language", "en-US");
		httpHeader = requestHeaders;		
		return httpHeader;
	}
	
	private static ArrayList<Integer> stickerList=null;
	
	
	
	private static ArrayList<Integer> emoList=null;
	
	public static ArrayList<Integer> getEmoList(){
		
		if(emoList != null)
			return emoList;
		
		emoList = new ArrayList<Integer>();
		
		emoList.add(R.drawable.happy);
		emoList.add(R.drawable.sad);
		emoList.add(R.drawable.winking);
		emoList.add(R.drawable.biggrin);
		emoList.add(R.drawable.battingeyelashes);
		emoList.add(R.drawable.confused);
		emoList.add(R.drawable.lovestruck);
		emoList.add(R.drawable.blushing);
		emoList.add(R.drawable.tounge);
		emoList.add(R.drawable.kiss);
		
		emoList.add(R.drawable.surpized);
		emoList.add(R.drawable.angry);
		emoList.add(R.drawable.smug);
		emoList.add(R.drawable.cool);
		emoList.add(R.drawable.worried);
		emoList.add(R.drawable.devil);
		emoList.add(R.drawable.crying);
		emoList.add(R.drawable.laughing);
		emoList.add(R.drawable.straightface);
		emoList.add(R.drawable.raisedeyebrows);
		
		emoList.add(R.drawable.angel);
		emoList.add(R.drawable.nerd);
		emoList.add(R.drawable.talktothehand);
		emoList.add(R.drawable.sleepy);
		emoList.add(R.drawable.rollingeyes);
		emoList.add(R.drawable.sick);
		emoList.add(R.drawable.donttellanyone);
		emoList.add(R.drawable.notalking);
		emoList.add(R.drawable.clown);
		emoList.add(R.drawable.silly);
		
		emoList.add(R.drawable.yawn);
		emoList.add(R.drawable.drooling);
		emoList.add(R.drawable.thinking);
		emoList.add(R.drawable.doh);
		emoList.add(R.drawable.applause);
		emoList.add(R.drawable.pig);
		emoList.add(R.drawable.cow);
		emoList.add(R.drawable.monkey);
		emoList.add(R.drawable.chicken);
		emoList.add(R.drawable.rose);
		
		emoList.add(R.drawable.goodluck);
		emoList.add(R.drawable.flag);
		emoList.add(R.drawable.pumpkin);
		emoList.add(R.drawable.coffee);
		emoList.add(R.drawable.idea);
		emoList.add(R.drawable.skull);
		emoList.add(R.drawable.bug);
		emoList.add(R.drawable.alien);
		emoList.add(R.drawable.frustrated);
		emoList.add(R.drawable.cowboy);
		
		emoList.add(R.drawable.praying);
		emoList.add(R.drawable.hypnotised);
		emoList.add(R.drawable.moneyeyes);
		emoList.add(R.drawable.whistling);
		emoList.add(R.drawable.liar);
		emoList.add(R.drawable.punch);
		emoList.add(R.drawable.peacesign);
		emoList.add(R.drawable.shameonyou);
		emoList.add(R.drawable.dancing);
		emoList.add(R.drawable.bighug);
		
		return emoList;
	}
	
	public static String USER_ID_KEY = "user_id";
	public static String USER_CODE_KEY = "user_code";
	public static String USER_DOB_KEY = "user_dob";
	public static String USER_EMAIL_KEY = "user_email";
	public static String USER_FULLNAME_KEY = "user_fullname";
	public static String USER_GENDER_KEY = "user_gender";
	public static String USER_AVATAR_KEY = "user_avatar";
	public static String USER_STATUS_KEY = "user_status";
	public static String USER_ACTIVE_KEY = "user_active";
	public static String USER_CHARGE_KEY = "user_charge";
	
	private static String TAG = "Constanta";
	
	public static void setUserData(Context cntx){
		
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(cntx);
		SharedPreferences.Editor settingsEditor = settings.edit();
		Map<String, String> formParameters = new HashMap<String, String>();		
		String jid = settings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, null).replace("@"+Constanta.chatDomain, "");	

		formParameters.put("username", jid);
		formParameters.put("targetname", jid);
		String getProfileUrl = Constanta.apiUserProfile;
		try {			
			String profileRequest = HttpUtilsPost.post(getProfileUrl, Constanta.requestHeaders(), formParameters, null);
			
			JSONObject jsonResponse = new JSONObject(profileRequest);	
			String status = jsonResponse.getString("STATUS");
			if(status.trim().equalsIgnoreCase("SUCCESS")){				
				
				JSONObject profileDetail = new JSONObject(jsonResponse.getString("DATA"));	
				
				String userId = profileDetail.getString("user_id");
				String userCode = profileDetail.getString("user_code");
				String dob = profileDetail.getString("dob");
				String email = profileDetail.getString("email");
				String fullname = profileDetail.getString("fullname");
				String gender = profileDetail.getString("gender");
				String avatar = profileDetail.getString("avatar");
				String userStatus = profileDetail.getString("status");
				String active = profileDetail.getString("active");
				
				settingsEditor.putString(Constanta.USER_ID_KEY, userId);
				settingsEditor.putString(Constanta.USER_CODE_KEY, userCode);
				settingsEditor.putString(Constanta.USER_DOB_KEY, dob);
				settingsEditor.putString(Constanta.USER_EMAIL_KEY, email);
				settingsEditor.putString(Constanta.USER_FULLNAME_KEY, fullname);
				settingsEditor.putString(Constanta.USER_GENDER_KEY, gender);
				settingsEditor.putString(Constanta.USER_AVATAR_KEY, avatar);
				settingsEditor.putString(Constanta.USER_STATUS_KEY, userStatus);
				settingsEditor.putString(Constanta.USER_ACTIVE_KEY, active);				
				settingsEditor.commit();				
			}			
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			Log.e(TAG, e1.getMessage());
			e1.printStackTrace();
		} catch (ProtocolException e1) {
			// TODO Auto-generated catch block
			Log.e(TAG, e1.getMessage());
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			Log.e(TAG, e1.getMessage());
			e1.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		}
	}
	public static File getCurrentAvatar(){
		File sdCard = Environment.getExternalStorageDirectory();
        File imgFile = new  File(sdCard, "avatar_cache.jpg");
        return imgFile;
	}
	
	public static int currentChatCount = 0;
	public static String[] rightActionTitle = new String[]{"Chat", "Delete Chat"};
	public static String[] searchActionTitle = new String[]{"View Profile", "Add as Friend"};
	
}
